package com.itheima.servlet;

import com.itheima.common.ProductConst;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Product;
import com.itheima.service.ProductService;
import com.itheima.service.impl.ProductServiceImpl;
import com.itheima.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYan
 * @Date 2021/1/16
 */
@WebServlet("/product")
public class ProductServlet extends BaseServlet{

    private ProductService productService = new ProductServiceImpl();

    //查询所有方法
    public void getHotAndNew(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //调用service层方法
        List<Product> hotList = productService.findHot();
        List<Product> newList = productService.findNew();

        //将两个list集合 添加到map集合中
        Map<String,List<Product>> map = new HashMap<>();
        map.put("hotList",hotList);
        map.put("newList",newList);

        //转换json并响应数据
        printResult(Result.SUCCESS,"查询最新最热成功",map,response);
    }

    //根据ID查询商品详情
    public void getProductById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String pid = request.getParameter("pid");
        //调用service
        Product product = productService.finById(pid);
        //判断
        if (product != null){
            //查询成功 返回数据
            printResult(Result.SUCCESS,"查询成功",product,response);
        } else {
            //查询失败
            printResult(Result.FAILS,"没有该商品",response);
        }
    }

    //分页查询
    public void getProductByPage (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //需要cid  currentPage  pageSize这三个参数
        //1.获取请求参数cid
        String cid = request.getParameter("cid");

        //2.当前页码默认值设为1,再通过请求参数获取,并转为int类型
        int currentPage = 1;
        //获取请求参数 当前页currentPage,String类型
        String currentPageStr = request.getParameter("currentPage");
            System.out.println("看一下获取到的currentPage是:"+currentPageStr); //"null"
            System.out.println("长度是：" + currentPageStr.length()); //4

        //前端传过来的参数是 "null",是个字符串,所以不能只判断是否为空
        if (currentPageStr!=null && !currentPageStr.equals("null")){
            //转换成int类型
            currentPage = Integer.parseInt(currentPageStr);
        }

        //3.设置每页显示条数pageSize,ProductConst类里设置里默认参数 PAGE_SIZE=12
        int pageSize = ProductConst.PAGE_SIZE;

        //调用Service层方法,获取pageBean对象
        PageBean<Product> pageBean = productService.findByPage(cid, currentPage, pageSize);

        //判断
        if (pageBean != null){
            //查询成功
            //pageBean转成json数据写回响应
            printResult(Result.SUCCESS,"分页查询成功",pageBean,response);
        } else {
            //查询失败
            printResult(Result.FAILS,"分页查询失败",response);
        }

    }
}
